package com.happle.gcmclient.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.happle.gcmclient.R;
import com.happle.gcmclient.obj.LngObj;

/**
 * Created by Inna on 02.11.13.
 */
public class SettingsLngAdapter extends BaseAdapter {

	public interface DeleteLanguageListener {
		void onDeleteLanguage(LngObj language);
	}

	private String TAG = "SettingsLngAdapter.java";
	private Activity context;
	private List<LngObj> listLngObj;
	private static LayoutInflater inflater = null;
	private DeleteLanguageListener deleteListener;

	public SettingsLngAdapter(Activity activity, List<LngObj> listLanguageObj,
			DeleteLanguageListener deleteListener) {
		context = activity;
		this.listLngObj = listLanguageObj;
		this.deleteListener = deleteListener;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return listLngObj.size();
	}

	@Override
	public Object getItem(int position) {
		return listLngObj.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View row = convertView;
		LngHolder holder;
		if (row == null) {
			holder = new LngHolder();
			row = inflater.inflate(R.layout.settings_lng_row, null);
			holder.lblLngName = (TextView) row
					.findViewById(R.id.settings_row_lng_name);
			holder.btnLngDel = (Button) row
					.findViewById(R.id.settings_row_lng_delete);
			holder.rlWrapper = (RelativeLayout) row
					.findViewById(R.id.settings_row_wrapper);
			holder.languageObj = listLngObj.get(position);
			row.setTag(holder);
		} else {
			holder = (LngHolder) row.getTag();
		}

		final LngObj item = listLngObj.get(position);
		if (item != null) {
			holder = (LngHolder) row.getTag();
			// holder.btnLngDel.setTag(holder.languageObj);
			holder.lblLngName.setText(item.get_lng_name());
		} else {
			Log.i(TAG, "Null Object !");
		}
		if (getCount() == 1) {
			holder.btnLngDel.setVisibility(View.GONE);
		} else
			holder.btnLngDel.setVisibility(View.VISIBLE);

		holder.btnLngDel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				deleteListener.onDeleteLanguage(item);
			}
		});
		return row;
	}

	public static class LngHolder {
		LngObj languageObj;
		TextView lblLngName;
		Button btnLngDel;
		RelativeLayout rlWrapper;
	}
}
